package demo5;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student> {
    public String name;
    public int age;
    public double score;
    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
    @Override
    public int compareTo(Student o) {
/*        if(this.age - o.age > 0) {
            return 1;
        }else if(this.age - o.age == 0) {
            return 0;
        }else {
            return -1;
        }*/
        return o.age - this.age;
    }
}

public class Test {
    public static void main3(String[] args) {
        Student student = new Student("zhangs",12,30.4);
        Student student1 = new Student("li",23,233.4);
        if(student.compareTo(student1) > 0) {
            System.out.println("学生1的年龄大于学生2的年龄");
        }else if (student.compareTo(student1) < 0) {
            System.out.println("学生1的年龄小于学生2的年龄");
        }else {
            System.out.println("学生1和学生2的年龄相同");
        }
    }
    //自定义类型比较大小，需要实现比较
    public static void main(String[] args) {
        Student[] array = new Student[3];
        array[0] = new Student("zhangsan",12,45.3);
        array[1] = new Student("lisi",22,65.3);
        array[2] = new Student("wangwu",32,55.3);
        System.out.println("排序前"+Arrays.toString(array));
        //Arrays.sort(array);
        bubbleSort(array);
        //报错，不知道按照什么数据排序
        System.out.println("排序后"+Arrays.toString(array));

    }
    public static void bubbleSort(Comparable[] comparables) {
        for(int i = 0; i < comparables.length-1; i++) {
            for(int j = 0; j < comparables.length-1-i; j++) {
                //if(comparables[j] > comparables[j+1]) {
                if (comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }
    public static void main1(String[] args) {
        int[] array = {1,4,2,6,3,5};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
